package org.meruvian.yama.dao;

import javax.persistence.TypedQuery;

import org.apache.commons.lang.StringUtils;
import org.meruvian.yama.Item;
import org.meruvian.yama.persistence.EntityListWrapper;
import org.meruvian.yama.persistence.LogInformation.StatusFlag;
import org.meruvian.yama.persistence.access.PersistenceDAO;
import org.meruvian.yama.persistence.utils.PagingUtils;
import org.springframework.stereotype.Repository;

@Repository
public class ItemDomain extends PersistenceDAO<Item> {

	public EntityListWrapper<Item> findItems(String name, String description,
			String orderby, String order, int max, int page, String condition) {
		String ql = "SELECT d FROM "
				+ entityClass.getName()
				+ " d WHERE d.name like :name";
		TypedQuery<Item> query = entityManager.createQuery(ql, entityClass);

		query.setParameter("name", "%" + name + "%");
		if (max > 0) {
			query.setMaxResults(max);
		}
		query.setFirstResult(page);
		EntityListWrapper<Item> paging = new EntityListWrapper<Item>();
		paging.setCurrentPage(page);
		paging.setLimit(max);
		paging.setRowCount(getRowCount());
		paging.setEntityList(query.getResultList());
		return paging;
	}

}
